// qt for 安卓权限申请
#include "mainwindow.h"
#include <QPermission>
#include <QDebug>
#include <qapplication.h>
#include <QtCore>
#include <QMessageBox>

void AddPermission(QString str_permission){
#ifndef WIN32
    QJniObject::callStaticMethod<void>("org/qtproject/QtAndroid",
                                       "AddPermission",
                                       "(Ljava/lang/String;)V",
                                       QJniObject::fromString(str_permission));
#endif
}
void ApplyPermission(){
#ifndef WIN32
    QJniObject::callStaticMethod<void>("org/qtproject/QtAndroid",
                                       "ApplyPermission",
                                       "()V");
#endif
}

void MainWindow::Permission(){
#ifdef WIN32
    //qt6自带的权限申请，一般只有基础权限（比如相机、录音、蓝牙、通讯录等权限）
    // camera
    QCameraPermission cameraPermission;
    switch (qApp->checkPermission(cameraPermission)) {
    case Qt::PermissionStatus::Undetermined:
        qApp->requestPermission(cameraPermission, this, &MainWindow::Permission);
        return;
    case Qt::PermissionStatus::Denied:
        qWarning("Camera permission is not granted!");
        return;
    case Qt::PermissionStatus::Granted:
        break;
    }
    // microphone
    QMicrophonePermission microphonePermission;
    switch (qApp->checkPermission(microphonePermission)) {
    case Qt::PermissionStatus::Undetermined:
        qApp->requestPermission(microphonePermission, this, &MainWindow::Permission);
        return;
    case Qt::PermissionStatus::Denied:
        qWarning("Microphone permission is not granted!");
        return;
    case Qt::PermissionStatus::Granted:
        break;
    }
#else
    // 添加SD卡权限
    AddPermission("android.permission.READ_EXTERNAL_STORAGE");
    AddPermission("android.permission.WRITE_EXTERNAL_STORAGE");
    AddPermission("android.permission.MANAGE_EXTERNAL_STORAGE");
    AddPermission("android.permission.READ_MEDIA_AUDIO");
    AddPermission("android.permission.READ_MEDIA_IMAGES");
    AddPermission("android.permission.READ_MEDIA_VIDEO");
    AddPermission("android.permission.WRITE_MEDIA_VIDEO");
    AddPermission("android.permission.WRITE_MEDIA_IMAGES");
    AddPermission("android.permission.WRITE_MEDIA_VIDEO");
    // 添加相机权限
    //AddPermission("android.permission.CAMERA");

    // 申请权限
    ApplyPermission();
#endif
}
